package cn.wolfcode.luowowo.service.impl;



import cn.wolfcode.luowowo.domain.Destination;

import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.DestinationObjcet;

import cn.wolfcode.luowowo.repository.DestinationRepository;
import cn.wolfcode.luowowo.repository.RegionRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private MongoTemplate template;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private RegionRepository regionRepository;
    @Override
    public Destination get(String id) {
        return  destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        List<Destination> list = destinationRepository.findAll();
        return list;
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        Region region = regionService.get(rid);
        List<String> refIds = region.getRefIds();
        List<Destination> list = destinationRepository.findByIdIn(refIds);
        return list;
    }

    @Override
    public Page<Destination> query(DestinationObjcet qo) {
        //创建查询对象
        Query query = new Query();
        //分页查询
//        long count = template.count(query, Destination.class);
//        if(count==0){
//            return Page.empty();
//        }
        PageRequest request = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");

        if(StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else{
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        //关键字查询
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(".*"+qo.getKeyword()+".*"));
        }

//        query.with(request);

       return DBHelper.query(template,Destination.class,query,request);



//        List<Destination> list = template.find(query, Destination.class);
//        return new PageImpl<>(list,request,count);
    }


    @Override
    public void saveOrUpdate(Destination destination) {
        if(destination.getId()==null){
            this.save(destination);
        }else{
            this.update(destination);
        }
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public  List<Destination> getToasts(String parentId) {
        List<Destination> list = new ArrayList();
        if(!StringUtils.hasLength(parentId)){
            return Collections.emptyList();
        }
        cerateToast(list,parentId);
        //2级反转
        Collections.reverse(list);
        return list;
    }

    @Override
    public List<Destination> search(String regionId) {
        List<Destination> list  = new ArrayList();
        if("-1".equals(regionId)){
            list = destinationRepository.findByParentName("中国");
        }else{
            Region region = regionService.get(regionId);
            List<String> ids = region.getRefIds();
            list = destinationRepository.findByIdIn(ids);
        }
        for (Destination destination : list) {
            PageRequest of = PageRequest.of(0, 5);
            String id = destination.getId();
            List<Destination> chidren = destinationRepository.findByParentId(id, of);
            destination.setChildren(chidren);
        }
        return list;
    }


    private void cerateToast(List<Destination> list,String parentId){
        Destination destination = this.get(parentId);
        list.add(destination);
        if(StringUtils.hasLength(destination.getParentId())){
            cerateToast(list,destination.getParentId());
        }
    }



}
